package com.example.permission.controller;

import com.example.permission.entity.Permission;
import com.example.permission.entity.Role;
import com.example.permission.entity.User;
import com.example.permission.service.PermissionService;
import com.example.permission.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private PermissionService permissionService;

    /**
     * 查询所有角色
     * @param map
     * @return
     */
    @GetMapping("/role")
    public String allRole(ModelMap map){
        List<Role> all = roleService.findAll();
        map.put("roles",all);
        return "/role/roles";
    }

    /**
     * 跳转到添加页面
     * @return
     */
    @GetMapping("/roles")
    public String add(){
        return "/role/roleAdd";
    }

    /**
     * 添加方法
     * @param map
     * @param role
     * @return
     */
    @PostMapping("/roles")
    public String toAdd(ModelMap map, Role role){
        Integer add = roleService.add(role);
        map.put("add",add);
        List<Role> all = roleService.findAll();
        map.put("roles",all);
        return "/role/roles";
    }

    /**
     * 跳转到修改页面
     * @param id
     * @param map
     * @return
     */
    @GetMapping("/role/{id}")
    public String update(@PathVariable Integer id,ModelMap map){
        Role idAll = roleService.getIdAll(id);
        map.put("role",idAll);
        return "/role/roleAdd";
    }

    /**
     * 修改方法
     * @param role
     * @param map
     * @return
     */
    @PutMapping("/roles")
    public String toUpdate(Role role, ModelMap map){
        roleService.update(role);
        List<Role> all = roleService.findAll();
        map.put("roles",all);
        return "/role/roles";
    }

    /**
     * 删除方法
     * @param id
     * @param map
     * @return
     */
    @DeleteMapping("/role/{id}")
    public String delete(@PathVariable Integer id, ModelMap map){
        roleService.delete(id);
        List<Role> all = roleService.findAll();
        map.put("roles",all);
        return "/role/roles";
    }

    /**
     * 更新角色权限
     */
    @PatchMapping("/role/{id}")
    public String toPatch(@PathVariable Integer id,ModelMap map){
        List<Permission> all = permissionService.findAll();
        map.put("permissions",all);
        List<Integer> permissionIds = roleService.getPermissionIdsByRoleId(id);
        map.put("permissionIds",permissionIds);
        map.put("id",id);
        return "/role/permission";
    }

    @GetMapping("/role/{id}/{permissionIds}")
    public String pathRole(@PathVariable Integer id,@PathVariable String permissionIds,ModelMap map){
        System.out.println(id+permissionIds);
        String[] arr = permissionIds.split(",");//分割传进来的id
        List<Integer> ids = roleService.getPermissionIdsByRoleId(id);//获取当前权限的id
        for(String s : arr){
            boolean flag = false;
            for(Integer integer:ids){ //循环之前已有的全选
                if(s.equals(integer+"")){ //然后作比较，如果刚穿进来的和已有的相同那就删除已有的，把新传进来的，添加进去
                    ids.remove(integer);
                    flag = true;
                    break;
                }
            }
            //如果flag是false那就证明没走上面的判断，
            if(!flag){
                //方法进去后，执行添加方法
                roleService.addPermissionIdByRoleId(id,Integer.valueOf(s));
            }
        }
        //执行删除方法
        ids.forEach(permissionId -> roleService.removePermissionIdByRoleId(id,permissionId));
        List<Role> all = roleService.findAll();
        map.put("roles",all);
        return "/role/roles";
    }

    @GetMapping("/roless/{ids}")
    public String del(ModelMap map,@PathVariable String ids){
        String[] split = ids.split(",");
        for (String i: split) {
            roleService.delete(Integer.parseInt(i));
        }
        List<Role> all = roleService.findAll();
        map.put("roles",all);
        return "/role/roles";
    }
}
